package com.bonc.ioc.filter;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 单点登录拦截器
 *
 * @author：ycy
 * @date：created in 2020/5/19 9:15
 * @update：
 * @version:
 */
//@Component
public class CasFilter implements Filter {

    private Logger logger = LoggerFactory.getLogger(CasFilter.class);

//    @Autowired
//    private RedisTemplate redisTemplate;

//
//    @Value("${sso.skipUrls}")
//    private String skipUrls;


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;

        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String contextPath = request.getContextPath();

        String requestURI = request.getRequestURI();

        requestURI = requestURI.replaceAll(contextPath, "");


        // 处理跳过拦截的路径
        boolean flag = true;
//        if (skipUrls != null && !"".equals(skipUrls)) {
//            String[] skipUrlArray = skipUrls.split(",");
//            for (String skipUrl : skipUrlArray) {
//                if (requestURI.equals(skipUrl) | requestURI.startsWith("/activity/getDetailInfo/") | requestURI.startsWith("/activitySign/sign") | requestURI.startsWith("/activitySign/undo") | requestURI.startsWith("/favorites") | requestURI.contains("/app/getDetailInfo/") | requestURI.startsWith("/activity/selectActivityInformationByPage")) {
//                    flag = false;
//                }
//            }
//        }

        // 拦截后处理
        if (flag) {
            String token = null;
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (int i = 0; i < cookies.length; i++) {
                    String name = cookies[i].getName();
                    if ("p-user-token".equals(name)) {
                        token = cookies[i].getValue();
                    }
                }
            }
            if (token != null && !"".equals(token)) {
//                // 验证token是否过期
//                boolean isOverdue = verificationToken(token);
//                if (isOverdue) {
//                    //获取请求中的codes
//                    String codes = request.getParameter("codes");
//                    Object o = redisTemplate.opsForValue().get(token);
//                    if (o != null) {
//                        redisTemplate.opsForValue().set(token, o, 1800L, TimeUnit.SECONDS);
//                    }
//                    System.err.println("======================***********" + o);
//                    Object userInfo = redisTemplate.opsForValue().get(token);
//                    JSONObject jsonObject = (JSONObject) JSON.toJSON(userInfo);
//                    String orgLevel = jsonObject.getString("orgLevel");
//                    JSONArray orgCodes = jsonObject.getJSONArray("orgCodes");
                filterChain.doFilter(request, response);
//                } else {
//                    login(response);
//                }
            } else {
                login(response);
            }
        } else {

            filterChain.doFilter(request, response);
        }
        return;
    }

    @Override
    public void destroy() {

    }

    /**
     * 验证token
     *
     * @param token
     * @return
     * @throws
     * @author ycy
     * @date 2020/6/10 17:00
     */
//    private boolean verificationToken(String token) {
//
//        Object o = redisTemplate.opsForValue().get(token);
//        if (o != null) {
//            logger.error(o.toString());
//            redisTemplate.opsForValue().set(token, o, 1800L, TimeUnit.SECONDS);
//
//            return true;
//        }
//        return false;
//
//    }

    /**
     * 跳转登录页面
     *
     * @param response
     * @return
     * @throws
     * @author
     * @date 2020/4/22 11:48
     */
    public void login(HttpServletResponse response) throws IOException {
        response.setStatus(403);
    }
}
